package com.rayo.accesscontrollercopy.module.main;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.app.BaseFragment;
import com.rayo.accesscontrollercopy.model.db.bean.KeyData;
import com.rayo.accesscontrollercopy.model.db.dao.KeyDataDao;
import com.rayo.accesscontrollercopy.model.db.dao.KeyDataImp;
import com.rayo.accesscontrollercopy.module.adapter.KeyAdapter;
import com.rayo.accesscontrollercopy.module.adapter.MainFragmentAdapter;
import com.rayo.accesscontrollercopy.module.login.LoginActivity;

import java.util.List;


/**
 * Created by lyc-pc on 17/4/20.
 */

public class MainFragment extends BaseFragment {
    private RecyclerView mRlvFragmentKey;
    RelativeLayout mRlFragmentMainGet;
    RelativeLayout mRlFragmentMainRet;
    private boolean onKeyDown = true;
    private MainFragmentAdapter mAdapter;
    private List<KeyData> keyDataList;
    KeyDataDao keyDataDao = new KeyDataImp();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void setupViews() {
        mRlvFragmentKey = rootView.findViewById(R.id.recycleview);
        mRlFragmentMainRet = (   RelativeLayout) rootView.findViewById(R.id.rl_fragment_main_ret);
        mRlFragmentMainGet = (   RelativeLayout) rootView.findViewById(R.id.rl_fragment_main_get);
    }

    @Override
    protected void initialized() {
        MainActivity.main=true;
        onKeyDown = true;
        mRlFragmentMainGet.setOnClickListener(this);
        mRlFragmentMainRet.setOnClickListener(this);
        keyDataList = keyDataDao.getAll();
        mAdapter = new MainFragmentAdapter(keyDataList, getContext());
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(getContext(), 8);
        mRlvFragmentKey.setLayoutManager(mGridLayoutManager);
        mRlvFragmentKey.setAdapter(mAdapter);
    }

    public boolean getKeyDown() {
        return onKeyDown;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        onKeyDown = false;
        MainActivity.main=false;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_fragment_main_get:
                Intent intent = new Intent(getContext(), LoginActivity.class);
                intent.putExtra(LoginActivity.EXTRA_DATA, LoginActivity.OPEN);
                startActivity(intent);
                break;
            case R.id.rl_fragment_main_ret:
                mSwitchFra.swicth();
                break;
        }
    }

    private ISwitchFra mSwitchFra;

    public interface ISwitchFra {
        void swicth();

    }

    public void setmISwicthFra(ISwitchFra swicthFra) {
        mSwitchFra = swicthFra;
    }
}
